const url = 'https://ainengli.hzjrsw.com'

interface options {
  url: string
  data?: object
}

function beOver() {
  // 重新登录
}

function get(options: options): Promise<{} | undefined> | void {
  return new Promise<{} | undefined>((resolve, reject) => {
    let token = uni.getStorageSync('token')
    uni.request({
      // url: uni.getSystemInfoSync().uniPlatform == 'mp-weixin' ? 'http://192.168.100.50:8082' + options.url : url + options.url,
      url: url + options.url,
      method: 'GET',
      data: options.data,
      header: {
        'Content-Type': 'application/json',
        // Authorization: store.state.token,
        Authorization: token,
        orgId: 2
      },
      dataType: 'json',
      success: (respoones: any) => {
        if (respoones.statusCode == '200') {
          if (respoones.data.code != 0) {
            uni.showToast({
              icon: 'error',
              title: respoones.data.msg
            })
            reject(respoones.data)
          }
          return resolve(respoones.data)
        } else {
          reject()
        }
      },
      fail: () => {
        uni.showToast({
          title: '请求失败',
          icon: 'none'
        })
        throw new Error('请求失败')
      }
    })
  })
}

function post(options: options): Promise<{} | undefined> | void {
  return new Promise<{} | undefined>((resolve, reject) => {
    let token = uni.getStorageSync('token')
    // #ifdef MP-ALIPAY
    uni.request({
      url:
        uni.getSystemInfoSync().uniPlatform == 'mp-weixin'
          ? 'http://192.168.100.50:8082' + options.url
          : url + options.url,
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        // Authorization: store.state.token,
        Authorization: token,
        orgId: 2
      },
      timeout: 30000,
      data: options.data,
      dataType: 'json',
      responseType: options.url == '/chat/speak/' ? 'arraybuffer' : 'text',
      success: (respones: any) => {
        if (respones.statusCode == 200) {
          if (respones.data.code === 401) {
            uni.showModal({
              title: '提示',
              content: '您当前的登陆信息已过期，点击确定重新登陆',
              showCancel: false,
              success: (res) => {
                beOver()
              }
            })
            reject(respones.data)
          }
          return resolve(respones.data)
        } else {
          console.log('服务器异常')
          reject()
        }
      },
      fail: (err) => {
        uni.showToast({
          title: '请求失败',
          icon: 'error'
        })
        console.log(err)
        reject()
        // throw new Error('请求失败');
      },
      complete: () => {}
    })
  })
}

interface uploadOptions {
  url: string
  file: any
  name?: string
}

function upload(file: string): Promise<{} | undefined> | void {
  return new Promise<{} | undefined>((resolve, reject) => {
    uni.showLoading({
      mask: true,
      title: '上传中...'
    })
    let date = new Date().getTime()
    let formData = {
      busiKind: 'leave',
      schoolOrgId: '1001000001',
      timestamp: date
    }
    uni.uploadFile({
      url: uploadUrl,
      filePath: file,
      fileType: 'image',
      name: 'files',
      formData: formData,
      success: (respones: any) => {
        let res = JSON.parse(respones.data)
        if (respones.statusCode === 200) {
          resolve(res)
        } else {
          reject()
        }
      },
      fail: () => {
        throw new Error('上传失败')
      },
      complete: () => {
        uni.hideLoading()
      }
    })
  })
}

export default {
  get: get,
  post: post,
  upload: upload
}
